//
//  TTDiamondDetailSectionView.m
//  TaoTuo
//
//  Created by 周晨晔 on 2019/9/18.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDiamondDetailSectionView.h"

@class TTTitleView;

typedef void(^ClickBlock)(TTTitleView * _Nullable view);

@interface TTTitleView : UIView

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, assign) BOOL isSelect;
@property (nonatomic, copy) ClickBlock block;

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title subTitle:(NSString *)subTitle clickBlock:(ClickBlock)clickBlock;

- (void)setSelect:(BOOL)select;

@end

@implementation TTTitleView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title subTitle:(NSString *)subTitle clickBlock:(ClickBlock)clickBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.block = clickBlock;
        
        _titleLabel = [UILabel labelWithTitle:title color:[UIColor blackColor] font:[UIFont systemFontOfSize:18]];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.width.equalTo(self);
            make.height.mas_equalTo(30);
        }];
        
        _subTitleLabel = [UILabel labelTextCenterWithTitle:subTitle color:UIColorHex(999999) fontSize:11];
        _subTitleLabel.layer.cornerRadius = 9 * kScale;
        [self addSubview:_subTitleLabel];
        
        [_subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(2);
            make.centerX.equalTo(self);
            make.height.mas_equalTo(18 * kScale);
            make.width.mas_equalTo(55 * kScale);
        }];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickView)];
        [self addGestureRecognizer:tap];
        
    }
    return self;
}

- (void)setSelect:(BOOL)select
{
    self.isSelect = select;
    if (select) {
        self.titleLabel.textColor = kMainColor;
        self.subTitleLabel.layer.backgroundColor = kMainColor.CGColor;
        self.subTitleLabel.textColor = [UIColor whiteColor];
    } else {
        self.titleLabel.textColor = [UIColor blackColor];
        self.subTitleLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
        self.subTitleLabel.textColor = UIColorHex(999999);
    }
}

- (void)clickView
{
    if (self.block) {
        self.block(self);
    }
}

@end

@interface TTDiamondDetailSectionView ()

@property (nonatomic, strong) TTTitleView *oneView;
@property (nonatomic, strong) TTTitleView *twoView;
@property (nonatomic, strong) TTTitleView *threeView;

@property (nonatomic, copy) SelectBlock selectBlock;

@property (nonatomic, strong) NSArray *array;

@end

@implementation TTDiamondDetailSectionView

- (instancetype)initWithFrame:(CGRect)frame selectTitleBlock:(SelectBlock)selectTitleBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        self.selectBlock = selectTitleBlock;
        
        [self setupViews];
        
//        // 默认选中女戒
        if (self.selectBlock) {
            self.selectBlock(self, @"女戒");
        }
        
    }
    return self;
}

- (void)setViewStatusWithView:(TTTitleView *)view
{
    for (TTTitleView *tv in self.array) {
        if (view == tv) {
            [view setSelect:YES];
        } else {
            [tv setSelect:NO];
        }
    }
}

- (void)setupViews
{
    CGFloat w = 80 * kScale;
    @weakify(self)
    _oneView = [[TTTitleView alloc] initWithFrame:CGRectMake(5, 0, w, self.height) title:@"女戒" subTitle:@"配个女戒" clickBlock:^(TTTitleView *view) {
        @strongify(self)
        if (!view.isSelect && self.selectBlock) {
            self.selectBlock(self, @"女戒");
        }
        [self setViewStatusWithView:view];
        
    }];
    // 默认选中
    [_oneView setSelect:YES];
    [self addSubview:_oneView];
    
    
    _twoView = [[TTTitleView alloc] initWithFrame:CGRectMake(self.oneView.right, 0, w, self.height) title:@"男戒" subTitle:@"配个男戒" clickBlock:^(TTTitleView *view) {
        @strongify(self)
        if (!view.isSelect && self.selectBlock) {
            self.selectBlock(self, @"男戒");
        }
        [self setViewStatusWithView:view];
    }];
    [self addSubview:_twoView];
    
    _threeView = [[TTTitleView alloc] initWithFrame:CGRectMake(self.twoView.right, 0, w, self.height) title:@"吊坠" subTitle:@"配个吊坠" clickBlock:^(TTTitleView *view) {
        @strongify(self)
        if (!view.isSelect && self.selectBlock) {
            self.selectBlock(self, @"吊坠");
        }
        [self setViewStatusWithView:view];
    }];
    [self addSubview:_threeView];
    
    
    self.array = @[_oneView, _twoView, _threeView];
    
    [LLLineUtilies addLineInView:self startPoint:CGPointMake(_oneView.right, 10) endPoint:CGPointMake(_oneView.right, self.height - 10) lineColor:UIColorHex(EDEDED)];
    
    [LLLineUtilies addLineInView:self startPoint:CGPointMake(_twoView.right, 10) endPoint:CGPointMake(_twoView.right, self.height - 10) lineColor:UIColorHex(EDEDED)];
    
    [LLLineUtilies addLineInView:self startPoint:CGPointMake(_threeView.right, 10) endPoint:CGPointMake(_threeView.right, self.height - 10) lineColor:UIColorHex(EDEDED)];
    
}



@end
